<template>
  <div id="info-dialog" class="dialog">
    <div class="dialog-wrap">
      <div class="dialog-header">
        <span class="heder-title">{{ props.title }}详情</span>
        <el-icon><component is="Close" @click="emit('close', false)" /></el-icon>
      </div>
      <div class="dialog-content">
        <div class="warp">
          <div class="data-box" v-for="(item, key) in props.data" :key="key">
            <template v-if="key !== 'event_type1'">
              <div class="label">
                <span>{{ dictionary[key] ? dictionary[key] : key }}</span>
              </div>
              <div class="name-value hae-input" v-if="key == 'record_time'">{{ timeFormathmsUTC(item) }}</div>
              <div class="name-value hae-input" v-else-if="key == 'resource_type'">{{ item ? $t(('resource.' + item) as string) : '--' }}</div>
              <div class="name-value hae-input" v-else-if="key !== 'details' && key !== 'message'">
                {{ $t(item) ? $t(item) : '--' }}
              </div>
              <template v-else>
                <el-tooltip effect="dark" placement="bottom" :disabled="!isShowTooptip">
                  <template #content>
                    <p slot="content" style="width: 600px; word-break: break-all">{{ $t(item) }}</p>
                  </template>
                  <div v-tool-tip id="creation" @mouseenter="visibilityChange($event)" style="line-height: 28px">
                    {{ $t(item) ? $t(item) : '--' }}
                  </div>
                </el-tooltip>
              </template>
            </template>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { timeFormathmsUTC } from '@/utils/formatter'
  import { reactive, ref, onUnmounted } from 'vue'
  import { nextTick } from 'vue'
  const props = defineProps({
    data: Object,
    title: String
  })
  const emit = defineEmits(['close'])
  const dictionary: { [key: string]: string | { [key: string | number]: string } } = {
    message: '事件消息',
    notice_state: '事件消息发布状态',
    details: '事件详情',
    name: '事件名',
    id: '事件ID',
    trace_service: '服务组件',
    resource_type: '资源类型',
    resource_name: '资源名',
    resource_id: '资源ID',
    trace_state: '事件级别',
    event_type: '事件类型',
    source_ip: '来源IP',
    record_time: '记录时间'
  }

  const bodyClose = (e: any) => {
    if (e.target.className === 'dialog') {
      emit('close')
    }
  }
  document.addEventListener('click', bodyClose)
  onUnmounted(() => {
    document.removeEventListener('click', bodyClose)
  })
  let isShowTooptip = ref<Boolean>(false)
  const vToolTip = {
    mounted: (el: any) => {
      const line = el.offsetHeight / parseInt(el.style.lineHeight)
      if (!line) return false
      if (line > 3) {
        el.setAttribute('class', 'over')
      }
    }
  }
  const visibilityChange = (event: any) => {
    nextTick(() => {
      if (event.target.getAttribute('class') == 'over') {
        isShowTooptip.value = true
      } else {
        isShowTooptip.value = false
      }
    })
  }
</script>

<style lang="scss" scoped>
  #info-dialog {
    z-index: 20;
    .el-input {
      width: 222px;
      height: 30px;
    }
  }
  .dialog {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.4);
    overflow: auto;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .dialog-wrap {
    border-radius: 4px;
    background-color: #fff;
  }
  .dialog-header {
    padding: 15px 26px;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid $border-color;
    .el-icon {
      cursor: pointer;
    }
  }
  ::-webkit-scrollbar {
    width: 8px !important;
  }
  ::-webkit-scrollbar-thumb {
    background-color: #e1e3e4 !important;
  }
  ::-webkit-scrollbar-track-piece {
    background: #fff !important;
  }

  .dialog {
    justify-content: flex-end;
  }

  .dialog-header {
    padding: 17px 40px;

    .heder-title {
      font-size: 18px;
      color: #1d1d1f;
      font-weight: bold;
    }
  }

  .warp {
    padding: 0 40px;
  }
  .data-box {
    width: 100%;
    margin-bottom: 24px;
    display: flex;
    align-items: center;
    #creation {
      height: auto;
      word-break: break-all;
      cursor: pointer;
      color: $text-color;
    }
    .over {
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-line-clamp: 3;
      display: -webkit-box;
      -webkit-box-orient: vertical;
    }
    .label {
      display: inline-block;
      flex-shrink: 0;
      font-size: 14px;
      color: $action-color;
      line-height: 30px;
      height: 30px;
      margin-right: 20px;
      text-align: left;
      white-space: nowrap;

      span {
        display: inline-block;
        width: 130px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    .name-value {
      width: 560px;
      word-break: break-all;
      min-height: 30px;
      line-height: 30px;
      color: $text-color;
    }
  }
  .dialog-content {
    width: 100%;
    margin: 0 auto;
    padding: 30px 0 0 0;
    flex: 1;
    overflow-y: scroll;

    &::-webkit-scrollbar-thumb {
      background-color: #e1e3e4;
    }

    &::-webkit-scrollbar-track-piece {
      background: #fff;
    }
  }
  .dialog-wrap {
    width: 42%;
    height: 100%;
    display: flex;
    flex-direction: column;
    border-radius: 0;
  }
</style>
